package com.review.Test.Master_Worker;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.springframework.asm.Handle;

public class Worker implements Runnable{
	
	private ConcurrentLinkedQueue<Task> tasks;
	private ConcurrentHashMap<String, Object> result;

	public ConcurrentLinkedQueue<Task> getTasks() {
		return tasks;
	}
	public void setTasks(ConcurrentLinkedQueue<Task> tasks) {
		this.tasks = tasks;
	}
	public ConcurrentHashMap<String, Object> getResult() {
		return result;
	}
	public void setResult(ConcurrentHashMap<String, Object> result) {
		this.result = result;
	}

	@Override
	public void run() {
		while (true) {
			Task task = tasks.poll();
			if (task == null) {
				break;
			}
			
			try {
				int money = Handle(task);
				result.put("task"+ task.getId(), money);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		
	}
	private int Handle(Task task) throws InterruptedException {
		Thread.sleep(500);
		return task.getMoney();
	}

}
